{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download Data\n",
    "\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/geoai/blob/main/docs/examples/download_data.ipynb)\n",
    "\n",
    "## Install Package\n",
    "To use the `geoai-py` package, ensure it is installed in your environment. Uncomment the command below if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install geoai-py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import Libraries\n",
    "These modules allow downloading NAIP imagery and extracting building data statistics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap\n",
    "from geoai.download import (\n",
    "    download_naip,\n",
    "    download_overture_buildings,\n",
    "    extract_building_stats,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define Bounding Box\n",
    "Define the geographic extent (longitude and latitude) for data downloads."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[47.6526, -117.5923], zoom=16)\n",
    "m.add_basemap(\"Esri.WorldImagery\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default ROI will be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bbox = m.user_roi_bounds()\n",
    "if bbox is None:\n",
    "    bbox = (-117.6029, 47.65, -117.5936, 47.6563)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download NAIP Imagery\n",
    "Fetch NAIP aerial imagery for the specified bounding box. The `max_items` parameter limits the number of downloaded files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download NAIP imagery for the specified region\n",
    "downloaded_files = download_naip(\n",
    "    bbox=bbox,\n",
    "    output_dir=\"naip_data\",\n",
    "    max_items=1,\n",
    "    # year=2020,\n",
    ")\n",
    "\n",
    "print(f\"Downloaded {len(downloaded_files)} files.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download Building Data\n",
    "Retrieve building footprint data in GeoJSON format within the bounding box. The `verbose` flag provides detailed output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download buildings\n",
    "data_file = download_overture_buildings(\n",
    "    bbox=bbox,\n",
    "    output=\"buildings.geojson\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extract Building Statistics\n",
    "If the building data file is successfully downloaded, extract and display relevant statistics such as area, count, and footprint details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stats = extract_building_stats(data_file)\n",
    "print(stats)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize Datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_raster(\"naip_data/m_4711720_sw_11_060_20230701_20230911.tif\", layer_name=\"NAIP\")\n",
    "m.add_geojson(\"buildings.geojson\", layer_name=\"Buildings\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image](https://github.com/user-attachments/assets/64475787-8ba1-4d12-8fe6-29fbb1dac2ed)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
